package org.aom.litebean.module.security;

import java.util.List;
import java.util.Map;

import org.aom.entity.security.Role;
import org.aom.entity.security.User;
import org.aom.litebean.module.base.BeforeRenderBaseBean;
import org.aom.service.security.IRoleManager;
import org.aom.service.security.IUserManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;
import org.operamasks.faces.util.Browser;

@ManagedBean(name = "module.security.userLinkRolesBean", scope = ManagedBeanScope.SESSION)
public class UserLinkRolesBean extends BeforeRenderBaseBean {

	private Long selectedModuleId;

	private User selectUser;

	public static String SELECTED_MODULE_ID = "selectedModuleId";

	@Bind(id = "uncheckedDataGrid", attribute = "binding")
	private UIDataGrid uncheckedDataGrid;

	@Bind(id = "checkedDataGrid", attribute = "binding")
	private UIDataGrid checkedDataGrid;

	@ManagedProperty("#{roleManager}")
	private IRoleManager roleManager;

	@ManagedProperty("#{userManager}")
	private IUserManager userManager;

	@Bind(id = "uncheckedDataGrid", attribute = "dataProvider")
	public GridDataProvider getUnDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return roleManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				List<Role> un = roleManager.getAll();
				List<Role> ck = getSelectedUser().getRoleList();
				un.removeAll(ck);
				return un.toArray();
			}

			public int getTotalCount() {
				return roleManager.getAll().size()
						- getSelectedUser().getRoleList().size();
			}
		};
	}

	@Bind(id = "checkedDataGrid", attribute = "dataProvider")
	public GridDataProvider getCkDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return roleManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				return getSelectedUser().getRoleList().toArray();
			}

			public int getTotalCount() {
				return getSelectedUser().getRoleList().size();
			}
		};
	}

	private User getSelectedUser() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
		return userManager.get(selectedModuleId);
	}

	@Action(id = "del")
	public void del() {
		selectUser = getSelectedUser();
		Object[] rs = checkedDataGrid.getSelectedValues();
		if (rs == null || rs.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object role : rs) {
				selectUser.getRoleList().remove((Role) role);
			}
			userManager.save(selectUser);
			uncheckedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.reload();
			// Browser.execClientScript("window.parent.BtnJsvar.fireEvent('click');");

		}
	}

	@Action(id = "add")
	public void add() {
		selectUser = getSelectedUser();
		Object[] rs = uncheckedDataGrid.getSelectedValues();
		if (rs == null || rs.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object role : rs) {
				selectUser.getRoleList().add((Role) role);
			}
			userManager.save(selectUser);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
			// Browser.execClientScript("window.parent.BtnJsvar.fireEvent('click');");
		}
	}
}
